package com.coship.vasms.adstorm.webservice.advertise;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import com.coship.vasms.adstorm.common.BeanHelper;
import com.coship.vasms.adstorm.common.CommonUtils;
import com.coship.vasms.adstorm.common.DateHelper;
import com.coship.vasms.adstorm.common.log.JLogger;
import com.coship.vasms.adstorm.common.log.LoggerFactory;
import com.coship.vasms.adstorm.model.Arrangenotify;
import com.coship.vasms.adstorm.service.ArrangeNotifyService;
import com.coship.vasms.adstorm.webservice.WebServiceConstant;
import com.coship.vasms.adstorm.webservice.advertise.vo.AdvertiseDisplayReq;
import com.coship.vasms.adstorm.webservice.advertise.vo.ArrangDisplay;
import com.coship.vasms.adstorm.webservice.position.Response;

public class AdvertiseNotifyDisplayReqStub {
	private static final JLogger logger = LoggerFactory
		.getLogger(AdvertiseNotifyPlayReqStub.class);
	
	public static Configuration configuration;
	public static String FILE_NAME = "adnotifydisplay.properties";
	private RPCServiceClient serviceClient = null;
	private List<ArrangDisplay> ADRList = null;
	private ArrangeNotifyService arrangeNotifyService = null;
	private QName opAddEntry = null;
	private Integer pageStart;
	private Integer pageSize;
	
	/**
	 * 
	 * Description 	: ���������ļ�����ȡConfiguration����
	 *
	 * @return
	 *
	 */
	static{
		try {
			configuration = new PropertiesConfiguration(CommonUtils
					.getConfigPath()
					+ CommonUtils.getSysFileSeparator()
					+ FILE_NAME);
			if (configuration == null) {
				logger.error("ϵͳ�����ļ�" + FILE_NAME + "������!");
				throw new IllegalArgumentException("ϵͳ�����ļ�"
						+ FILE_NAME + "������!");
			}
		} catch (ConfigurationException e) {
			logger.error("�������ļ�" + FILE_NAME + "���ִ���");
			throw new IllegalArgumentException("�������ļ�" + 
					FILE_NAME + "���ִ���");
		}
	}
	
	public AdvertiseNotifyDisplayReqStub(ArrangeNotifyService anservice, Integer pageStart, Integer pageSize){
		this.arrangeNotifyService = anservice;
		this.pageStart = pageStart;
		this.pageSize = pageSize;
	}
	
	/**
	 * 
	 * Description 	: ����ͣ��֪ͨ����
	 *
	 *
	 */
	public void notifyDisplayAdvertise(){
		logger.info("��ʼ������Ҫͣ��֪ͨ�Ĺ��");
		try{
			//��ʼ������Ϣ
			init();
			
			//����ͣ��֪ͨ
			dealDisplayNotify();
			
			//����������֪ͨ������Ҫ����ͣ��֪ͨ�ļ�¼
			dealArrangeDisplayNotify();
			
			
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		logger.info("��������Ҫͣ��֪ͨ�Ĺ��");
	}
	
	/**
	 * 
	 * Description 	: ��ʼ������Ϣ
	 *
	 *
	 */
	private void init(){
		logger.info("��ʼ��ʼ��������Ҫͣ��֪ͨ�Ĺ��");
		try{
			String url = (String)configuration
				.getProperty("advertise.notify.display.stop.url");
			String action = (String)configuration
				.getProperty("advertise.notify.display.stop.action");
			String packages = (String)configuration
				.getProperty("advertise.notify.display.qname.stop.package");
			String methods = (String)configuration
				.getProperty("advertise.notify.display.qname.stop.method");
			long timeout = Long.valueOf((String)configuration
					.getProperty("advertise.notify.display.timeout"));
			
			serviceClient = new RPCServiceClient();
			
			Options options = new Options();
			options.setTimeOutInMilliSeconds(timeout);
			options.setTo(new EndpointReference(url));
			options.setAction(action);
			
			serviceClient.setOptions(options);
			opAddEntry = new QName(packages, methods);
			
			//��ȡ���Ź��(ArrangDisplay)Ϊad_arrange��ad_advertisement}��
			
//			arrangeNotifyService = (ArrangeNotifyService)BeanHelper
//				.getInstance().getBean("arrangeNotifyService");
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		logger.info("�����ʼ��������Ҫͣ��֪ͨ�Ĺ��");
	}
	
	/**
	 * 
	 * Description 	: ����ͣ��֪ͨ
	 *
	 *
	 */
	private void dealDisplayNotify(){
		//��ȡ��������
		Integer n = Integer
			.valueOf((String)configuration.getProperty("advertise.notify.display.stop.endtime"));
		String time = DateHelper.getDate(n);
		ADRList = arrangeNotifyService.queryDisplayReq(time, pageStart, pageSize);
		
		if(ADRList != null){
			for(int i=0;i<ADRList.size();i++){
				ArrangDisplay ad = ADRList.get(i);
				
				//��ȡ���͸����Ķ���AdvertiseDisplayReq
				AdvertiseDisplayReq adr = new AdvertiseDisplayReq();
				adr.setAdvertiseType(ad.getAdvertiseType());
				adr.setAgentType(ad.getAgentType());
				adr.setContent(ad.getContent());
				adr.setPositionBasicCode(ad.getPositionBasicCode());
				adr.setPositionInfoCode(ad.getPositionInfoCode());
				
				if(adr != null){
					//���񷵻ص���Ϣ����
					Response response = null;
					//��ȡ����ͣ��֪ͨ��ʷ��Ķ���ADArrange
					Arrangenotify arrNotify = new Arrangenotify();
					arrNotify.setArrangeId(ad.getArrangeId());
					arrNotify.setCreateTime(DateHelper.getDate());
					arrNotify.setNotifyStopNumber(1);
					try {
						//����ͣ��֪ͨ
						response = (Response)serviceClient.invokeBlocking(opAddEntry, new Object[]{ adr },
								new Class[] { Response.class })[0];
						if(response != null){
							if(response.getRetCode() != null && 
									response.getRetCode()
										.equals(WebServiceConstant.RESPONSE_SUCCESS))
							{
								arrNotify.setNotifyStopState(WebServiceConstant.NOTIFY_STOP_STATE_Y);
							}
							else{
								arrNotify.setNotifyStopState(WebServiceConstant.NOTIFY_STOP_STATE_N);
							}
							arrNotify.setNotifyEndFailMsg(response.getRetCode() 
										+ ":" +response.getMsg());
						}
						
						//�ڹ������֪ͨ�������¼
						arrangeNotifyService.save(arrNotify);
					} catch (AxisFault e) {
						logger.error("����֪ͨͣ�����ִ���");
						e.printStackTrace();
						throw new IllegalArgumentException("����֪ͨͣ�����ִ���");
					} finally{
						if(response == null){
							arrNotify.setNotifyStopState(WebServiceConstant.NOTIFY_STOP_STATE_N);
							arrNotify.setNotifyEndFailMsg("����֪ͨͣ������ʱ��");
							//�ڹ������֪ͨ�������¼
							arrangeNotifyService.save(arrNotify);
						}
					}
					System.out.println("����ͣ��֪ͨ���񷵻�ֵ:" + response.getMsg());
				}
			}
		}
	}
	
	/**
	 * 
	 * Description 	: ����������֪ͨ������Ҫ����ͣ��֪ͨ�ļ�¼
	 *
	 *
	 */
	private void dealArrangeDisplayNotify(){
		Integer number = Integer
			.valueOf((String)configuration.getProperty("advertise.notify.display.stop.number"));
		//��ȡ��������
		Integer n = Integer
			.valueOf((String)configuration.getProperty("advertise.notify.display.stop.endtime"));
		String time = DateHelper.getDate(n);
		
		List<ArrangDisplay> arrangeDisplayList = null;
		//��ѯ����֪ͨͣ��ʧ�ܵ��б�
		arrangeDisplayList = arrangeNotifyService.queryDisplayNotifyFail(time, number, pageStart, pageSize);
		if(arrangeDisplayList != null){
			for(int i=0;i<arrangeDisplayList.size();i++){
				ArrangDisplay ad = arrangeDisplayList.get(i);
				//��ȡ���͸����Ķ���AdvertiseDisplayReq
				AdvertiseDisplayReq adr = new AdvertiseDisplayReq();
				adr.setAdvertiseType(ad.getAdvertiseType());
				adr.setAgentType(ad.getAgentType());
				adr.setContent(ad.getContent());
				adr.setPositionBasicCode(ad.getPositionBasicCode());
				adr.setPositionInfoCode(ad.getPositionInfoCode());
				
				if(adr != null){
					//���񷵻ص���Ϣ����
					Response response = null;
					//��ȡ����ͣ��֪ͨ��ʷ��Ķ���ADArrange
					Arrangenotify arrNotify = arrangeNotifyService.queryByid(ad.getNotifyId());
					
					Integer stopNumber = arrNotify.getNotifyStopNumber();
					
					try {
						//����ͣ��֪ͨ
						while(stopNumber < number)
						{
							arrNotify.setLastNotifyStopTime(DateHelper.getDate());
							response = (Response)serviceClient.invokeBlocking(opAddEntry, new Object[]{ adr },
									new Class[] { Response.class })[0];
							if(response != null)
							{
								if(response.getRetCode() != null && 
										response.getRetCode()
											.equals(WebServiceConstant.RESPONSE_SUCCESS))
								{
									arrNotify.setNotifyStopState(WebServiceConstant.NOTIFY_STOP_STATE_Y);
									arrNotify.setNotifyStopNumber(stopNumber+1);
									break;
								}
								else{
									stopNumber++;
									arrNotify.setNotifyStopState(WebServiceConstant.NOTIFY_STOP_STATE_N);
									arrNotify.setNotifyStopNumber(stopNumber);
								}
							}
						}
						
						arrNotify.setNotifyEndFailMsg(response.getRetCode() 
								+ ":" +response.getMsg());
						
						arrangeNotifyService.update(arrNotify);
						
					} catch (AxisFault e) {
						throw new IllegalArgumentException("����֪ͨͣ�����ִ���");
					} finally{
						if(response == null){
							arrNotify.setNotifyStopState(WebServiceConstant.NOTIFY_STOP_STATE_N);
							arrNotify.setNotifyStopNumber(stopNumber+1);
							arrNotify.setNotifyEndFailMsg("����֪ͨͣ������ʱ��");
							//�ڹ������֪ͨ�������¼
							arrangeNotifyService.update(arrNotify);
						}
					}
					System.out.println("����ͣ��֪ͨ���񷵻�ֵ:" + response.getMsg());
				}
			}
		}
	}
}
